package com.example.demo.core.model;

import com.alibaba.fastjson.JSON;
import lombok.*;


import java.io.Serializable;

/**
 * @author yujie
 * @date 2021-04-19 21:34
 */
@Data
@NoArgsConstructor
@Builder(toBuilder = true)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class ResultBean<T> implements Serializable {

    @Builder.Default
    private int status = 200;

    @Builder.Default
    private String message = "";

    @Builder.Default
    private T data = (T)null;

    public ResultBean(CodeEnum codeEnum) {
        this.status = codeEnum.getStatus();
        this.message = codeEnum.getMessage();
    }

    public static ResultBean<Object> success() {
        return ResultBean.builder().build();
    }

    public static ResultBean<Object> returnObject(Object data) {
        return ResultBean.builder()
                .data(data).build();
    }

    public static ResultBean<Object> returnError(CodeEnum codeEnum) {
        return ResultBean.builder()
                .status(codeEnum.getStatus())
                .message(codeEnum.getMessage())
                .data(null).build();
    }

    public static ResultBean<Object> returnCustom(int status, String message, Object data) {
        return ResultBean.builder()
                .status(status)
                .message(message)
                .data(data).build();
    }

    public String toJson() {
        return JSON.toJSONString(this);
    }
}
